package leetcode.code1418;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {
	
	private static final String Table = "Table";

	public List<List<String>> displayTable(List<List<String>> orders) {
		Set<String> foods = new HashSet<>();
		Set<Integer> tables = new HashSet<>();
		Map<Integer, Map<String, Integer>> map = new HashMap<>();
		for (int i = 0; i < orders.size(); i++) {
			List<String> order = orders.get(i);
			Integer table = Integer.valueOf(order.get(1));
			String food = order.get(2);
			foods.add(food);
//			Map<String, Integer> tableOrder = map.getOrDefault(table, new HashMap<>());
//			tableOrder.put(food, tableOrder.getOrDefault(food, 0) + 1);
//			map.put(table, tableOrder);
			map.computeIfAbsent(table, key -> new HashMap<>()).put(food, map.get(table).getOrDefault(food, 0) + 1);
			tables.add(table);
		}
		List<String> foodsList = new ArrayList<>(foods);
		Collections.sort(foodsList);
		List<String> titles = new ArrayList<>();
		titles.add(Table);
		titles.addAll(foodsList);
		List<Integer> tablesList = new ArrayList<>(tables);
		Collections.sort(tablesList);
		List<List<String>> ans = new ArrayList<>();
		ans.add(titles);
		for (int i = 0; i < tablesList.size(); i++) {
			int table = tablesList.get(i);
			List<String> cur = new ArrayList<>();
			cur.add(String.valueOf(table));
			Map<String, Integer> tableOrder = map.get(table);
			for (String food : foodsList) {
				cur.add(String.valueOf(tableOrder.getOrDefault(food, 0)));
			}
			ans.add(cur);
		}
		return ans;
	}


	@Override
	public void debug4() {
		this.displayTable(H.str2intlist2("[[\"David\",\"3\",\"Ceviche\"],[\"Corina\",\"10\",\"Beef Burrito\"],[\"David\",\"3\",\"Fried Chicken\"],[\"Carla\",\"5\",\"Water\"],[\"Carla\",\"5\",\"Ceviche\"],[\"Rous\",\"3\",\"Ceviche\"]]"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}

